package love.server.handler;

import java.util.HashMap;

import love.server.JSONTCPServer;
import love.sock.model.BadRequestException;
import love.sock.model.EStatus;
import love.sock.model.Request;

import com.thetransactioncompany.jsonrpc2.JSONRPC2Error;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;
import com.thetransactioncompany.jsonrpc2.server.MessageContext;

public class ServerInfoHandler extends AbstractLoveHandler {
	
	public ServerInfoHandler(JSONTCPServer server) {
		super(server);
	}
	
	// Reports the method names of the handled requests
	public String[] handledRequests() {
		return new String[]{"server_info"};
	}
	
	@Override
	public String getMethod() {
		return JSONTCPServer.METHOD_GET;
	}

	@Override
	public JSONRPC2Response process(Request request, MessageContext requestCtx) throws BadRequestException {
		if (request.getAction().equals("server_info")) {

//			List<String> params = request.getParams();
//			if(params.size() < 1){
//				throw new BadRequestException(EStatus.ERROR400, "anonymous GET server_info");
//			}
//			
//			Partie partie = server.getPartieById(params.get(0));
//			if(partie == null){
//				throw new BadRequestException(EStatus.ERROR400, "Partie introuvable");
//			}
			
			JSONRPC2Response response = server.buildBasicResponse(request, "", EStatus.MESSAGE200, null);
//			response.setResult(partie.getGameInfo());
			
			HashMap<String, Object> result = new HashMap<String, Object>();
			
			result.put("protocol", "1.14");
			result.put("name", "ASA Love");
			result.put("games", server.getParties().size());
			
			response.setResult(result);
			
			return response;
		}
		else {
			// Method name not supported
			return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, request.getID());
		}
	}

	@Override
	public JSONRPC2Response process(JSONRPC2Request request,
			MessageContext requestCtx) {
		return null;
	}
}
